import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class DotRenderController extends ChangeNotifier {
  DotRenderController({List<int>? initDots, this.autoScroll, bool? loop})
      : _initDots = initDots,
        _loop = loop;

  static int row = 7;
  static int col = 26;

  ///hello: 0,39653760,38867520,64819776,38867520,39688576, 0
  static List<int> testDotsHello = [
    0,
    39653760,
    38867520,
    64819776,
    38867520,
    39688576,
    0
  ];

  ///26*7的矩阵
  final List<RxList<int>> dotRenderColors = List.generate(
      col, (index) => List.generate(row, (index) => 0, growable: false).obs,
      growable: false);
  Timer? _timer;

  ///滚动数据源
  final _scrollSourceList = <List<int>>[];
  int _scrollIndex = 0;

  ///初始化list
  final List<int>? _initDots;
  final bool? autoScroll;
  final bool? _loop;

  reset() {
    _timer?.cancel();
    initDotScreenData();
  }

  clean() {
    _timer?.cancel();
    for (int i = 0; i < col; i++) {
      for (int j = 0; j < row; j++) {
        dotRenderColors[i][j] = 0;
      }
    }
  }

  ///简单显示一个hello文本
  initDotScreenData() {
    //final data = [0, 39653760, 38867520, 64819776, 38867520, 39688576, 0];
    if (_initDots == null) return;
    for (int i = 0; i < col; i++) {
      for (int j = 0; j < row; j++) {
        dotRenderColors[i][j] = decimalToBinaryArray(_initDots![j], 26)[i];
      }
    }
  }

  ///滚动显示两个hello文本
  scrollShowTxt() {
    if (_initDots == null) return;
    _scrollIndex = 0;
    _scrollSourceList.clear();
    _scrollSourceList.addAll(transform27DotList());
    _timer?.cancel();
    _timer = Timer.periodic(150.milliseconds, (timer) {
      ///竖向赋值col -> 26
      for (int i = 0; i < col; i++) {
        for (int j = 0; j < row; j++) {
          int actIndex = _scrollIndex + i;
          if (actIndex >= _scrollSourceList.length) {
            actIndex -= _scrollSourceList.length;
          }
          dotRenderColors[i][j] = _scrollSourceList[actIndex][j];
        }
      }
      _scrollIndex++;
      if (_scrollIndex == _scrollSourceList.length) {
        if (_loop == true) {
          _scrollIndex = 0;
        } else {
          reset();
        }
      }
    });
  }

  appendScrollTxt() {
    _scrollSourceList.add([0, 0, 0, 0, 0, 0, 0]);
    _scrollSourceList.add([0, 0, 0, 0, 0, 0, 0]);
    _scrollSourceList.add([0, 0, 0, 0, 0, 0, 0]);
    _scrollSourceList.add([1, 1, 1, 1, 1, 1, 1]);
    _scrollIndex = 0;
  }

  ///转换成7点队列，list都是由竖着的7个点组成
  List<List<int>> transform27DotList() {
    ///解析成 7*26的二维数组
    final source = _initDots!;
    int sourceTransformLength = 26;

    ///生成7*26的矩阵
    final temp = List.generate(source.length,
        (index) => decimalToBinaryArray(source[index], sourceTransformLength));

    ///转置成26*7的矩阵
    return List.generate(
        temp[0].length, (i) => List.generate(temp.length, (j) => temp[j][i]));
  }

  List<int> decimalToBinaryArray(int decimal, int length) {
    String binaryString = (decimal.toUnsigned(32) >> (32 - length))
        .toRadixString(2)
        .padLeft(length, '0');
    return binaryString.split('').map(int.parse).toList();
  }

  @override
  void dispose() {
    clean();
    super.dispose();
  }

  void press() {
    if (autoScroll == true) {
      if (_timer != null && _timer!.isActive) {
        reset();
      } else {
        scrollShowTxt();
      }
    }
  }
}
